package com.teach.sys.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.teach.R;
import com.teach.annotation.CheckLogin;
import com.teach.sys.service.FileService;

@RestController
@RequestMapping("/sys/upload")
public class UploadApi {
	@Autowired
	private FileService fileService;

	@PostMapping
	@CheckLogin
	public R<?> upload(MultipartFile file,
			@RequestParam(defaultValue = "file") String pre,
			@RequestParam(defaultValue = "true") Boolean random) throws Exception {
		String uri = fileService.save(file.getInputStream(), file.getOriginalFilename(), pre, random);
		return R.ok().setData(uri);
	}

	@PostMapping("image")
	@CheckLogin
	public R<?> image(MultipartFile file,
			@RequestParam(defaultValue = "file") String pre,
			@RequestParam(defaultValue = "true") Boolean watermark,
			@RequestParam(defaultValue = "1200") Integer maxWidth,
			@RequestParam(defaultValue = "1200") Integer maxHeight
	) throws Exception {
		return R.ok().setData(fileService.saveImg(file.getInputStream(), file.getOriginalFilename(), pre, maxWidth,
				maxHeight, watermark ? fileService.getWatermark() : null));
	}

}
